package com.lxs.spring6.boot3.configure;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;

@Component
public class ScanRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        doScanComponent();
    }

    private void doScanComponent() {
        String rootPath = this.getClass().getResource("/").getPath();
        // 获取所有的class文件
        List<String> fileList = FileScanner.findFileByType(rootPath, null, FileScanner.TYPE_CLASS);
        // 将
        doFilter(rootPath, fileList);
        EnvInitializer.init(rootPath);
    }

    private void doFilter(String rootPath, List<String> fileList) {
        rootPath = FileScanner.getRealRootPath(rootPath);
        for (String fullPath : fileList) {
            String shortName = fullPath.replace(rootPath, "").replace(FileScanner.TYPE_CLASS, "");
            String packageFileName = shortName.replaceAll(Matcher.quoteReplacement(File.separator), "\\.");

            try {
                Class clazz = Class.forName(packageFileName);
                if (clazz.isAnnotationPresent(Component.class) || clazz.isAnnotationPresent(Service.class) || clazz.isAnnotationPresent(Controller.class) || clazz.isAnnotationPresent(Repository.class)) {
                    VariablePool.add(clazz);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}